package com.rmc.http.accesser;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.client.params.ClientParamBean;
import org.apache.http.conn.params.ConnRouteParamBean;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.*;
import org.apache.http.protocol.HTTP;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpAccesserParams {
	public static final String P_MAX_TOTAL_CONNECTIONS = "max_connections";
	public static final String P_MAX_CONNECTIONS_PER_ROUTE = "max_connections_per_route";
	final org.apache.http.params.HttpParams httpParams;
	private final HttpConnectionParamBean conParamBean;
	private final ClientParamBean clientParamBean;
	private final ConnRouteParamBean connRouteParamBean;


	public HttpAccesserParams() {
		httpParams = createHttpParams();
		conParamBean = new HttpConnectionParamBean(httpParams);
		clientParamBean = new ClientParamBean(httpParams);
		connRouteParamBean = new ConnRouteParamBean(httpParams);
	}

	private org.apache.http.params.HttpParams createHttpParams() {
		org.apache.http.params.HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params,
		                              HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
		HttpProtocolParams.setUseExpectContinue(params, true);
		HttpConnectionParams.setTcpNoDelay(params, true);
		HttpConnectionParams.setSocketBufferSize(params, 8192);
		HttpProtocolParams.setUserAgent(params,
		                                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
		return params;
	}

	public void setSoTimeout(int soTimeout) {
		conParamBean.setSoTimeout(soTimeout);
	}

	public void setConnectionTimeout(int connectionTimeout) {
		conParamBean.setConnectionTimeout(connectionTimeout);
	}

	public void setDefaultHeaders(Map<String, String> headers) {
		List<Header> headerList = new ArrayList<Header>();
		for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
			Header header = new BasicHeader(headerEntry.getKey(), headerEntry.getValue());
			headerList.add(header);
		}
		clientParamBean.setDefaultHeaders(headerList);
	}

	public void setDefaultHost(String uri) {
		HttpHost host = HttpUtils.getHostFromUri(uri);
		clientParamBean.setDefaultHost(host);
	}

	public void setDefaultProxy(String uri) {
		HttpHost defaultProxy = HttpUtils.getHostFromUri(uri);
		connRouteParamBean.setDefaultProxy(defaultProxy);
	}

	public void setTimeout(int timeout) {
		httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
	}

	public void setMaxTotalConnections(int maxConnections) {
		httpParams.setIntParameter(P_MAX_TOTAL_CONNECTIONS, maxConnections);
	}

	public void setConnectionsPerRoute(int maxConnectionsPerRoute) {
		httpParams.setIntParameter(P_MAX_CONNECTIONS_PER_ROUTE, maxConnectionsPerRoute);
	}

	public HttpAccesser createHttpAccesser() {
		return HttpAccesser.createHttpAccesser(this);
	}
}
